package com.topvision.controller.ar;

import com.alibaba.fastjson.JSONObject;
import com.topvision.annotation.IgnoreAuth;
import com.topvision.model.api.TokenApi;
import com.topvision.model.ar.OffFace;
import com.topvision.model.glass.Equipment;
import com.topvision.service.api.TokenApiService;
import com.topvision.service.ar.OffFaceService;
import com.topvision.service.glass.EquipmentService;
import com.topvision.util.*;
import com.topvision.vo.OffFaceDTO;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * 离线人脸数据
 *
 * @author shengwm
 * @email 179653076@qq.com
 * @date 2018-03-12 13:03:12
 */
@RestController
@RequestMapping("ar/offface")
public class OffFaceApiController {

    protected Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private OffFaceService offFaceService;

    @Autowired
    private EquipmentService equipmentService;

    @Autowired
    private TokenApiService tokenApiService;


    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("ar/offface:list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<OffFace> offFaceList = offFaceService.queryList(query);
        int total = offFaceService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(offFaceList, total, query.getLimit(), query.getPage());

        return R.success().put("page", pageUtil);
    }

    @RequestMapping("/findListByParam")
    public R findListByParam(@RequestParam Map<String, Object> params) {
        String btmac = params.get("btmac").toString();
        Equipment equipment = equipmentService.queryByGuid(btmac);
        if (equipment != null && equipment.getCompanycode() != null) {
            String companycode = equipment.getCompanycode();
            params.put("companycode", companycode);
            List<OffFaceDTO> offFaceList = offFaceService.findByParam(params);
            Map<String, OffFaceDTO> rawmap = new HashMap<>();
            List<Long> list = new ArrayList<>();
            for (OffFaceDTO oface : offFaceList) {
                if (rawmap.containsKey(oface.getFaceNumber())) {
                    if (oface.getCreateTime().getTime() > rawmap.get(oface.getFaceNumber()).getCreateTime().getTime()) {
                        list.add(rawmap.get(oface.getFaceNumber()).getId());
                        rawmap.put(oface.getFaceNumber(), oface);
                    }
                } else {
                    rawmap.put(oface.getFaceNumber(), oface);
                }
            }
            Collection<OffFaceDTO> set = rawmap.values();
            Long[] ids = list.toArray(new Long[0]);
            if (ids.length > 0) {
                offFaceService.deleteBatch(ids);
            }
            return R.success().put("offFaceList", set);
        }

        return R.success();

    }

    @RequestMapping("/delnopic")

    public R delnopic(@RequestBody Map<String, String> map) {
        String nums = map.get("facenumbers");
        String btmac = map.get("btmac");
        String[] nos = nums.split(",");
        Map<String, Object> parammap = new HashMap<>();

        Equipment equipment = equipmentService.queryByGuid(btmac);
        String companycode = equipment.getCompanycode();

        parammap.put("companycode", companycode);
        parammap.put("facenumbers", nos);

        offFaceService.delnopic(parammap);
        return R.success("success");
    }


    @RequestMapping(value = "/detectFace", method = RequestMethod.POST)
    @ApiOperation(value = "btmac && file文件对象", produces = MediaType.APPLICATION_JSON_VALUE, notes = "设备蓝牙地址或deviceid", response = R.class)
    @IgnoreAuth
    public R detectFace(HttpServletRequest request) {
        Map<String, Object> map = ArFileUtil.getFile(request, "file");
        String btmac = request.getParameter("btmac");
        Equipment equipment = equipmentService.queryByGuid(btmac);
        String companycode = equipment.getCompanycode();
        Map<String, Object> param = new HashMap<>();
        param.put("dbname", companycode);
        param.put("file", map.get("file"));

        JSONObject jsonObject = FaceApiUtil.recSmartFace(param);
        if (jsonObject != null && StringUtils.isNotBlank(jsonObject.get("class_name").toString())) {
            String code = jsonObject.get("class_name").toString();
            Map<String, Object> query = new HashMap<>();
            map.put("companycode", companycode);
            map.put("facenumber", code);
            List<OffFace> offFace = offFaceService.queryByParam(map);
            if (offFace.size() > 0) {
                return R.success().put("face", offFace.get(0)).put("score", jsonObject);
            }
        }
        return R.success();

    }


}
